package edu.miami.group.photologing;

import java.util.HashMap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;

public class MainActivity extends Activity implements SimpleCursorAdapter.ViewBinder, AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener{
	private Database database;
	private Cursor cursorFill;
	private long itemSelected;
	private final int THE_DIALOG = 1;
	private AlertDialog theDialog;
	private static final int ACTIVITY_SHOW_VIEW = 1;
	private static final int ACTIVITY_CREATE= 1;
	protected void onCreate(Bundle savedInstanceState) {

		String[] queryFields = {
				"picVid",
				"title"

		};

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		database = new Database(this);
		

		//Fetch all the data from the database and fill the ListView
		cursorFill = database.fetchAllPosts();
		ListView theList = (ListView) findViewById(R.id.list);
		//Need to understand how to query fields to get the listView running
		SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this,
				R.layout.little_picture_names_list_item, cursorFill, queryFields, new int[] {R.id.item_picture , R.id.item_description
		});
		myAdapter.setViewBinder(this);
		theList.setOnItemLongClickListener(this);
		theList.setOnItemClickListener(this);
		theList.setAdapter(myAdapter);
	}
	
	public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
		itemSelected = rowId;
		ContentValues returnObject  = database.getDataByID(rowId);
		Intent viewIntent = new Intent();
		viewIntent.setClassName("edu.miami.c06190115.group.photologging", "edu.miami.c06190115.group.photologging.View");
		viewIntent.putExtra("databaseObject", returnObject );
		startActivityForResult(viewIntent,ACTIVITY_SHOW_VIEW);	
	}
	
	
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	protected Dialog onCreateDialog(int dialogId) {
		AlertDialog.Builder dialogBuilder;
		LayoutInflater dialogInflator;

		dialogBuilder = new AlertDialog.Builder(this);
		switch (dialogId) {
		case THE_DIALOG:
			dialogInflator = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			View dialogView = dialogInflator.inflate(
					R.layout.little_picture_names_dialog_layout,
					(ViewGroup) findViewById(R.id.dialog_root));
			dialogBuilder.setView(dialogView);
			break;
		default:
			break;
		}
		theDialog = dialogBuilder.create();
		//theDialog.setOnDismissListener(this);
		return theDialog;

	}
	//-----------------------------------------------------------------------------
	//prepares the dialog
	protected void onPrepareDialog(int dialogId, Dialog dialog){
		
	}
	//dismiss button click handler
	public void myClickListener(View view){
		switch(view.getId()){
		case R.id.delete:
			ContentValues object = database.getDataByID(itemSelected);
			database.deletePost(object.getAsInteger("_id").intValue());
			cursorFill.requery();
			break;
		case R.id.cancel:
			theDialog.dismiss();
		case R.id.create:
			Intent createIntent = new Intent();
			createIntent.setClassName("edu.miami.c06190115.group.photologging", "edu.miami.c06190115.group.photologging.Create");
			startActivityForResult(createIntent,ACTIVITY_CREATE);
		} 
		
	}
	
	public boolean onItemLongClick(AdapterView<?> arg0, View view, int arg2,
			long id) {
		itemSelected = id;
		showDialog(THE_DIALOG);
		return true;
		}
		
		public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
			if (columnIndex == cursor.getColumnIndex("image_media_id")){
				//retrieves the thumbnail of the image from the MeidaStore
				Bitmap localBitmap = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), cursor.getInt(columnIndex), 3, null);
					((ImageView)view).setImageBitmap(localBitmap);
					return true;
		//	}
			}
			return false;
		}
		
		//-----------------------------------------------------------------------------
		//onDismiss after the dialog has exited starts the musicPlayer and stops the TextToSpeach engine
		

}
